<?php
namespace app\mouth\model;
use think\Model;
use think\Db;
class ShopUserModel extends Model{
    public static function order($maid,$type){
        $where['ma_id'] = $maid;
        switch ($type){
            case 1:
                $where['or_status'] = 1;
                break;
            case 2:
                $where['or_status'] = 2;
                break;
            case 3:
                $where['or_status'] = 3;
                break;
            case 4:
                $where['or_status'] = 4;
                break;
            default:
        }
      $date['order']        =  Db::name('order')->where($where)->order('or_id desc')->select();
      $date['singular']     = Db::name('order')->where('ma_id',$maid)->where('or_status',2)->whereTime('or_paytime','today')->count();
      $date['total']        = Db::name('order')->where('ma_id',$maid)->where('or_status',2)->count();
      return $date;
    }
    public static function adddiscount($date){
            $add = self::add($date);
            if (is_string($add)){
                return $add;
            }
            Db::name('discount')->insert($add);
            return true;
    }
    public static function addupdate($date){
        $add = self::add($date);
        if (is_string($add)){
            return $add;
        }
        Db::name('discount')->where('id',$date['id'])->update($add);
        return true;
    }
    public static function detailsorder($id){
        return Db::name('order')->where('or_id',$id)->find();
    }
    public static function appoint($ma_id){
        $where['openid'] = Db::name('order')->where('ma_id',$ma_id)->column('or_openid');
        return Db::name('wx_user')->where($where)->select();
    }
    public static function discountuser($uis,$id,$s_id,$state){
        $date['user_id'] = $uis;
        $date['volume_id'] = $id;
        $date['s_id'] = $s_id;
        $date['state'] = $state;
        $date['time'] = time();
        return Db::name('wx_user_discount')->insert($date);
    }
    public static function adddalete($id){
        return Db::name('discount')->where('id',$id)->delete();
    }
    public static function bus(){
        return Db::name('car_wash')->field('wa_standard,wa_model,wa_type')->select();
    }

    public static function discount($ma_id){
        return Db::name('discount')->whereTime('die_time','>',time())->where('ma_id',$ma_id)->select();
    }
    public static function businesspicture($ma_id){
        return Db::name('store')->field('ma_id,license,roadrescue,certificate')->where('ma_id',$ma_id)->find();
    }
    public static function product($ma_id){
        return Db::name('car_volume_store')->where('st_id',$ma_id)->select();
    }
    public static function add($date){
       if (empty($date['living_time']) || empty($date['die_time']) || empty($date['price']) || empty($date['name']) || empty($date['ma_id']) || empty($date['full']) || empty($date['state']) || empty($date['standard']) || empty($date['model'])){
           return '缺少参数';
       }
        if ($date['living_time'] > $date['die_time'] ){
            return '结束时间不能大于开始时间';
        }
        if ($date['full'] < $date['price']){
            return '优惠金额不能大于消费金额';
        }
        $value['living_time']   = strtotime($date['living_time']);
        $value['die_time']      = strtotime($date['die_time']);
        $value['price']         = $date['price'];
        $value['name']          = $date['name'];
        $value['ma_id']         = $date['ma_id'];
        $value['full']          = $date['full'];
        $value['state']         = $date['state'];
        $value['standard']      = $date['standard'];
        $value['model']         = $date['model'];
        $value['add_time']      = time();
        return $value;
    }
}